#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "cvector.h"
#include "clist.h"
#include "cstring.h"
#include <stdint.h>

int main(int argc, char** argv)
{
  /*32bit machines seem to have this as true*/
  /*matters for clist tests where i set ints as data in the void**/
//  assert(sizeof(int) == sizeof(void*));

  int i,j,k;
/*test cvector*/
/*  cvector_t* int_vector = new_cvector(sizeof(int));
  for(i=0; i<200; i++) {
    add_element(int_vector, &i);
  }
  for(i=0; i<200; i++) {
    int* e = element_at(int_vector, i);
    if(*e != i) {
      printf("FAILED cvector test elements\n");
      assert(0);
    }
  }
*/
/*end test cvector*/

/*test clist*/
  clist_t* int_list = new_clist();
  clist_push_back(int_list, (void*)5);
  clist_push_back(int_list, (void*)5912);
  clist_push_back(int_list, (void*)123912);
  clist_push_back(int_list, (void*)912);
  clist_push_back(int_list, (void*)12);
  clist_push_front(int_list, (void*)-5);

  int test_clist_data1[] = {-5,5,5912,123912,912,12};
  cnode_t* n = int_list->front;
  for(i=0; n != NULL; n = n->next, i++) {
    assert(test_clist_data1[i] == (int)n->data);
  }

  clist_reverse(int_list);
  n = int_list->front;
  for(i=5; n != NULL; n = n->next, i--) {
    assert(test_clist_data1[i] == (int)n->data);
  }
  clist_reverse(int_list);

  /* Warnings ok here on non 32 bit machines since we are just storing 
   * primitive types.
   */
  assert(clist_size(int_list) == 6);
  assert(12 == (int)clist_pop_back(int_list));
  assert(clist_size(int_list) == 5);
  assert(int_list->end->next == NULL);
  assert(-5 == (int)clist_pop_front(int_list));
  assert(clist_size(int_list) == 4);
  assert(912 == (int)clist_pop_back(int_list));
  assert(123912 == (int)clist_pop_back(int_list));
  assert(5912 == (int)clist_pop_back(int_list));
  assert(5 == (int)clist_pop_back(int_list));
  assert(0 == (int)clist_pop_back(int_list));
  assert(0 == (int)clist_pop_back(int_list));
  assert(0 == (int)clist_pop_front(int_list));


//  clist_pop_back(int_list);
//  assert(clist_size(int_list) == 3);
//  clist_pop_back(int_list);
//  assert(clist_size(int_list) == 2);
//  clist_pop_back(int_list);
//  assert(clist_size(int_list) == 1);
//  clist_pop_back(int_list);
//  assert(clist_size(int_list) == 0);
//  clist_pop_back(int_list);
//  assert(clist_size(int_list) == 0);
//  clist_pop_back(int_list);
//  assert(clist_size(int_list) == 0);
//  clist_pop_back(int_list);
//  assert(clist_size(int_list) == 0);
  
  /*end test clist*/

  char* hello = "hello";
  char* blah = "blah";
  char* three = "three";

  clist_t* list = new_clist();
  clist_push_back(list, hello);
  clist_push_back(list, blah);
  clist_push_front(list, three);
  assert(clist_size(list) == 3);

  clist_remove(list, 1);

  assert(clist_size(list) == 2);
  
  printf("--\n");
  n = list->front;
  while(n) {
    printf("%s\n", (char*)n->data);
    n = n->next;
  }
  printf("--\n");
  clist_free(list);

/*test cstring*/
  cstring_t* mystr = new_cstring();
  cstring_cat_oldstr(mystr, "four");
  assert(mystr->length == 4);

  cstring_t* s = new_cstring();
  cstring_cat_oldstr(s, "hello m    y  name is   bob   to day     is  ");
  clist_t* split_chars = cstring_split_oldstr(s, " ");
  assert(clist_size(split_chars) == 9);

  n = split_chars->front;
  while(n) {
    cstring_t* ss = (cstring_t*)n->data;
    printf("%s\n", ss->string);
    n = n->next;
  }
  clist_free(split_chars);
  
  clist_t* split = cstring_split_oldstr(s, ":");
  assert(split != NULL);
  assert(clist_size(split) == 1);
  s = (cstring_t*)split->front->data;
  printf("%s\n", s->string);
  
  mystr = new_cstring();
  cstring_cat_oldstr(mystr, "       ");
  split_chars = cstring_split_oldstr(mystr, " ");
  assert(split_chars == NULL);

  mystr = new_cstring();
  cstring_cat_oldstr(mystr, "  jobs    ");
  split = cstring_split_oldstr(mystr, " ");
  s = (cstring_t*)split->front->data;
  printf("%s\n", s->string);

  mystr = new_cstring();
  cstring_cat_oldstr(mystr, "hello today");
  assert(cstring_cmp_oldstr(mystr, "hello today") == 0);

  mystr = new_cstring();
  cstring_cat_oldstr(mystr, "hello");
  assert(cstring_cmp_oldstr(mystr, "h") != 0);
  assert(cstring_cmp_oldstr(mystr, "hello") == 0);

/*end test cstring*/

 return 0;
}
